/*************************************************************************
 * gOkey - A tile based game for Gnome.                                  *
 * Copyright (C) 2008  Onur Aslan  <onuraslan@gmail.com>                 *
 *                                                                       *
 * This program is free software: you can redistribute it and/or modify  *
 * it under the terms of the GNU General Public License as published by  *
 * the Free Software Foundation, either version 3 of the License, or     *
 * (at your option) any later version.                                   *
 *                                                                       *
 * This program is distributed in the hope that it will be useful,       *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 * GNU General Public License for more details.                          *
 *                                                                       *
 * You should have received a copy of the GNU General Public License     *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>. *
 *************************************************************************/

#include <glib/gi18n.h>
#include "gui.h"

/* this function starts a new single player game */
static gboolean
new_game (void) {
  GtkWidget *dialog;
  gboolean startgame = FALSE;
  if (game->status != PLAYING) {
    startgame = TRUE;
  } else {
    dialog = gtk_message_dialog_new (GTK_WINDOW (window), GTK_DIALOG_MODAL,
                                     GTK_MESSAGE_QUESTION, GTK_BUTTONS_YES_NO,
                                     _("Are you sure to start a new game?"));
    gtk_window_set_title (GTK_WINDOW (dialog), _("New game"));
    if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES) {
      startgame = TRUE;
    }
    gtk_widget_destroy (dialog);
  }
  if (startgame == TRUE) {
    init_game ();
    game->status = PLAYING;
    //clear_screen ();
    //draw_rack ();
    //draw_tiles_new_game ();
    change_status_bar_text (_("New game has been started"));
  }
  return TRUE;
}

static gboolean
about_dialog (void)
{
  const gchar *authors[] = {
    "Onur Aslan <onuraslan@gmail.com>",
    NULL
  };
  const gchar *license =
    "This program is free software: you can redistribute it and/or modify "
    "it under the terms of the GNU General Public License as published by "
    "the Free Software Foundation, either version 3 of the License, or "
    "(at your option) any later version."
    "\n\n"
    "This program is distributed in the hope that it will be useful, "
    "but WITHOUT ANY WARRANTY; without even the implied warranty of "
    "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the "
    "GNU General Public License for more details."
    "\n\n"
    "You should have received a copy of the GNU General Public License "
    "along with this program.  If not, see <http://www.gnu.org/licenses/>.";
  gtk_show_about_dialog (GTK_WINDOW (window),
                         "program-name", PACKAGE_NAME,
                         "name", PACKAGE_NAME,
                         "version", PACKAGE_VERSION,
                         "comments", _("A tile based game for Gnome."),
                         "copyright", "Copyright \xc2\xa9 2009 Onur Aslan",
                         "website", "http://code.google.com/p/gokey/",
                         "website-label", _("Project web site"),
                         "authors", authors,
                         "license", license,
                         "wrap_license", TRUE,
                         "title", PACKAGE_NAME,
                         NULL);
  return TRUE;
}

/* menu and toolbar */
static const char uiDescription[] =
  "<ui>"
  "  <menubar name='MainMenu'>"
  "    <menu action='GameMenu'>"
  "      <menuitem action='NewGame'/>"
  "      <separator/>"
  "      <menuitem action='Scores'/>"
  "      <separator/>"
  "      <menuitem action='Quit'/>"
  "    </menu>"
  "    <menu action='SettingsMenu'>"
  "      <menuitem action='Preferences'/>"
  "    </menu>"
  "    <menu action='HelpMenu'>"
  "      <menuitem action='Contents'/>"
  "      <menuitem action='About'/>"
  "    </menu>"
  "  </menubar>"
  "  <toolbar name='Toolbar'>"
  "    <toolitem action='NewGame'/>"
  "    <separator/>"
  "    <toolitem action='Scores'/>"
  "  </toolbar>"
  "</ui>";

/* menu and toolbar actions */
static const GtkActionEntry actions[] = {
  {"GameMenu", NULL, N_("_Game")},
  {"SettingsMenu", NULL, N_("_Settings")},
  {"HelpMenu", NULL, N_("_Help")},
  {"NewGame", GTK_STOCK_NEW, NULL, NULL, N_("Start a new game"),
      G_CALLBACK (new_game)},
  {"Scores", GTK_STOCK_DIALOG_QUESTION, N_("_Scores")},
  {"Quit", GTK_STOCK_QUIT, NULL, NULL, NULL, G_CALLBACK (gtk_main_quit)},
  {"Preferences", GTK_STOCK_PREFERENCES},
  {"Contents", GTK_STOCK_HELP},
  {"About", GTK_STOCK_ABOUT, NULL, NULL, NULL, G_CALLBACK (about_dialog)}
};

static void
create_menus (GtkUIManager *uiManager)
{
  GtkActionGroup *actionGroup;
  actionGroup = gtk_action_group_new ("group");
  gtk_action_group_add_actions (actionGroup, actions, G_N_ELEMENTS (actions),
                                NULL);
  gtk_ui_manager_insert_action_group (uiManager, actionGroup, 0);
  gtk_ui_manager_add_ui_from_string (uiManager, uiDescription, -1, NULL);
}

void
change_status_bar_text (const char *msg)
{
  gtk_statusbar_push (GTK_STATUSBAR (statusBar), 0, msg);
}

static void
init_gui (void)
{
  GtkWidget *vbox;
  GtkUIManager *ui_manager;
  GdkPixbuf *icon;
  char *filename;
  /* making pixbufs null */
  rackPixbuf = NULL;
  tilesPixbuf = NULL;
  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "Okey");
  gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size (GTK_WINDOW (window), WIDTH, HEIGHT);
  gtk_container_set_border_width (GTK_CONTAINER (window), 0);
  g_signal_connect (G_OBJECT (window), "delete_event",
                    G_CALLBACK (gtk_main_quit), NULL);
  /* main container vbox */
  vbox = gtk_vbox_new (FALSE, 0);
  gtk_container_add (GTK_CONTAINER (window), vbox);
  /* menus and toolbar */
  ui_manager = gtk_ui_manager_new ();
  create_menus (ui_manager);
  gtk_box_pack_start (GTK_BOX (vbox),
                      gtk_ui_manager_get_widget (ui_manager, "/MainMenu"),
                      FALSE, TRUE, 0);
  gtk_box_pack_start (GTK_BOX (vbox),
                      gtk_ui_manager_get_widget (ui_manager, "/Toolbar"),
                      FALSE, TRUE, 0);
  /* loading icon */
  filename = g_build_filename (ICONDIR, "gokey.png", NULL);
  icon = gdk_pixbuf_new_from_file (filename, NULL);
  gtk_window_set_default_icon (icon);
  g_free (filename);
  /* creating board */
  board = create_board ();
  gtk_window_set_focus (GTK_WINDOW (window), board);
  gtk_box_pack_start (GTK_BOX (vbox), board, TRUE, TRUE, 0);
  /* status bar */
  statusBar = gtk_statusbar_new ();
  gtk_widget_set_size_request (board, RACKWIDTH, RACKHEIGHT + BOARDMARGIN * 2);
  change_status_bar_text (_("Okey is ready..."));
  gtk_box_pack_start (GTK_BOX (vbox), statusBar, FALSE, TRUE, 0);
  gtk_widget_grab_focus (board);
  gtk_widget_show_all (window);
}

int
main (int argc, char *argv[])
{
  Game new_game = { 0 };
  game = &new_game;
  gtk_init (&argc, &argv);
  tiles = NULL;
  init_game ();
  init_gui ();
  gtk_main ();
  return 0;
}
